package com.huiqiao.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huiqiao.sys.domain.Menu;
import com.huiqiao.sys.domain.MenuVo;
import com.huiqiao.sys.domain.RoleMenu;
import com.huiqiao.sys.domain.RoleUser;
import com.huiqiao.sys.mapper.MenuMapper;
import com.huiqiao.sys.mapper.RoleMapper;
import com.huiqiao.sys.mapper.RoleMenuMapper;
import com.huiqiao.sys.mapper.RoleUserMapper;
import com.huiqiao.sys.service.MenuService;
import com.huiqiao.sys.utils.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author: yanliwei@foolish
 * @CreateTime: 2024-12-22  09:54
 */

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleUserMapper roleUserMapper;

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Menu> queryAllMenuForList(MenuVo menuVo) {
        return menuMapper.select(menuVo);
    }

    @Override
    public DataGridView queryAllMenu(MenuVo menuVo) {
        Page<Object> page = PageHelper.startPage(menuVo.getPage(), menuVo.getLimit());
        Weekend<Menu> weekend = Weekend.of(Menu.class);
        WeekendCriteria<Menu, Object> menuObjectWeekendCriteria = weekend.weekendCriteria();
        if (StrUtil.isNotBlank(menuVo.getTitle())){
            menuObjectWeekendCriteria.andLike(Menu::getTitle, "%" + menuVo.getTitle() + "%");
        }
        List<Menu> data = menuMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public void addMenu(MenuVo menuVo) {
        menuMapper.insertSelective(menuVo);
    }

    @Override
    public void deleteMenu(MenuVo menuVo) {
        /*//先删除子菜单
        //先判断是否有子菜单，通过 id 查询
        List<Menu> menus = menuMapper.select(new Menu().setPid(menuVo.getId()));
        //判断是否为空
        if (menus.size() > 0){
            throw new RuntimeException("请先删除子菜单");

        }*/
        menuMapper.deleteByPrimaryKey(menuVo.getId());
    }

    @Override
    public void updateMenu(MenuVo menuVo) {
        menuMapper.updateByPrimaryKeySelective(menuVo);
    }

    @Override
    public List<Menu> queryAll() {
        return menuMapper.selectAll();
    }

    @Override
    public Menu queryMenuById(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Menu> queryMenuByUserIdForList(Integer userid) {
        //通过用户id -> 角色id -> 菜单id
        List<RoleUser> roleUsers = roleUserMapper.select(new RoleUser().setUid(userid));
        List<Integer> roleIds = roleUsers.stream()
                .map(RoleUser::getRid)
                .filter(Objects::nonNull)
                .distinct()
                .collect(Collectors.toList());
        ArrayList<Menu> menus = new ArrayList<>();
        for (Integer role : roleIds) {
            List<RoleMenu> roleMenus = roleMenuMapper.select(new RoleMenu().setRid(role));
            List<Integer> menuIds = roleMenus.stream()
                    .map(RoleMenu::getMid)
                    .filter(Objects::nonNull)
                    .distinct()
                    .collect(Collectors.toList());
            List<Menu> menuList = menuIds.stream().map(menuMapper::selectByPrimaryKey).collect(Collectors.toList());
            menus.addAll(menuList);
        }
        //去重
        return menus.stream().distinct().collect(Collectors.toList());

    }


}
